package com.antler.luajavalibrary.luaext.ui;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.antler.luajavalibrary.application.MyApplication;

public class UIContext {
    protected static final String TAG = "antler_" + UIContext.class.getSimpleName();

    protected final Context mContext;

    protected JSONObject mGlobalStyles;

    protected boolean mIsAddWm = false;
    protected WindowManager.LayoutParams mParams;
    protected UIView mRootView = null;
    protected UIView mUIView = null;

    public static UIContext createContext(String layout, String style) {
        UIContext uiContext = new UIContext(MyApplication.getContext());
        if (uiContext != null) {
            uiContext.initWith(layout, style);
        }
        return uiContext;
    }

    public UIContext(Context context) {
        mContext = context;

        mParams = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT || Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            mParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        } else {
            mParams.type = WindowManager.LayoutParams.TYPE_TOAST;
        }

        mParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
//        mParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

        mParams.format = PixelFormat.RGBA_8888;

        mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

        // 居中
        mParams.gravity = Gravity.CENTER;
    }

    public void initWith(final String layout, final String style) {
        mGlobalStyles = JSON.parseObject(style);

        UI.getInstance().syncPost(new Runnable() {
            @Override
            public void run() {
                final JSONObject parser = JSON.parseObject(layout);
                if (parser != null) {
                    mRootView = LayoutInflater.from(UIContext.this).inflate(parser, null);
                    mRootView.setID("_root");
                }
            }
        });
    }

    public UIView createView(final String layout) {
        mUIView = null;

        UI.getInstance().syncPost(new Runnable() {
            @Override
            public void run() {
                final JSONObject parser = JSON.parseObject(layout);
                if (parser != null) {
                    mUIView = LayoutInflater.from(UIContext.this).inflate(parser, null);
                }
            }
        });

        return mUIView;
    }

    public void show() {
        UI.getInstance().asyncPost(new Runnable() {
            @Override
            public void run() {
                if (mRootView == null) {
                    return;
                }

                if (!mIsAddWm) {
                    UI.getInstance().getWindowManager().addView((View) mRootView, mParams);

                    mIsAddWm = true;
                }
            }
        });
    }

    public void close() {
        UI.getInstance().asyncPost(new Runnable() {
            @Override
            public void run() {
                if (mRootView == null) {
                    return;
                }

                if (mIsAddWm) {
                    UI.getInstance().getWindowManager().removeView((View) mRootView);
                }

                mIsAddWm = false;
            }
        });
    }

    public UIView getRootView() {
        return mRootView;
    }

    public UIView findView(String id) {
        if (mRootView instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) mRootView;
            View view = viewGroup.findViewWithTag(id);
            if (view instanceof UIView) {
                return (UIView) view;
            }
        }
        return null;
    }

    public Context getContext() {
        return mContext;
    }

    public JSONObject getGlobalStyle(String className) {
        JSONObject style = null;

        do {
            if (mGlobalStyles == null) {
                break;
            }

            style = mGlobalStyles.getJSONObject(className);

        } while (false);
        return style;
    }

    public JSONObject getPseudoStyle(String pseudoName) {
        JSONObject style = null;

        do {
            if (mGlobalStyles == null) {
                break;
            }

            style = mGlobalStyles.getJSONObject(pseudoName);

        } while (false);
        return style;
    }
}
